package com.jit.monitor.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jit.baselib.ui.base.BaseActivity;
import com.jit.baselib.ui.base.BasePresenter;
import com.jit.baselib.util.PerfectClickListener;
import com.jit.baselib.util.ui.UIUtils;
import com.jit.monitor.R;
import com.jit.monitor.commom.AppConstant;
import com.jit.monitor.presenter.EditCmAtPresenter;
import com.jit.monitor.view.EditCmNameAtView;

import butterknife.BindView;

/**
 * @author crazyZhangxl on 2018-12-21 18:07:58.
 * Describe:
 */

public class EditCmNameActivity extends BaseActivity<EditCmNameAtView,EditCmAtPresenter> implements EditCmNameAtView {
    @BindView(R.id.ivToolbarNavigation)
    ImageView mIvToolbarNavigation;
    @BindView(R.id.vToolbarDivision)
    View mVToolbarDivision;
    @BindView(R.id.tvToolbarTitle)
    TextView mTvToolbarTitle;
    @BindView(R.id.tv_publish_now)
    TextView mTvPublishNow;
    @BindView(R.id.edContent)
    EditText mEdContent;
    @BindView(R.id.ivDelete)
    ImageView mIvDelete;


    private String cameraID,cameraName;
    private int index;
    @Override
    protected void init() {
        if (getIntent() != null){
            cameraID =  getIntent().getStringExtra(AppConstant.CAMERAID);
            cameraName = getIntent().getStringExtra(AppConstant.CAMERANAME);
            index = getIntent().getIntExtra(AppConstant.CAMERA_INDEX,-1);
        }
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_edit_info;
    }

    @Override
    protected EditCmAtPresenter createPresenter() {
        return new EditCmAtPresenter(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mTvPublishNow.setVisibility(View.VISIBLE);
        mTvToolbarTitle.setText("编辑摄像头名称");
        mEdContent.setText(cameraName);
        mEdContent.setSelection(cameraName.length());
    }

    @Override
    protected void initData() {

    }

    public static void showCameraNameActivity(Activity context, int index,String cameraID, String cameraName){
        Intent intent = new Intent(context,EditCmNameActivity.class);
        intent.putExtra(AppConstant.CAMERA_INDEX,index);
        intent.putExtra(AppConstant.CAMERAID,cameraID);
        intent.putExtra(AppConstant.CAMERANAME,cameraName);
        context.startActivityForResult(intent,AppConstant.RECODE_FROM_MAIN_TO_CM);
    }

    @Override
    protected void initListener() {
        mIvToolbarNavigation.setOnClickListener(v -> onBackPressed());

        mIvDelete.setOnClickListener(v -> mEdContent.setText(""));


        mEdContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(mEdContent.getText().toString().trim())){
                    mIvDelete.setVisibility(View.GONE);
                }else {
                    mIvDelete.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        mTvPublishNow.setOnClickListener(new PerfectClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                cameraName =  mEdContent.getText().toString();
                if (TextUtils.isEmpty(cameraName)){
                    UIUtils.showToast("请输入摄像头名称!");
                    return;
                }
                mPresenter.submitCameraName(cameraID,cameraName);
            }
        });
    }



    @Override
    public void editNameSuccess() {
        Intent intent = new Intent();
        intent.putExtra(AppConstant.CAMERANAME,cameraName);
        intent.putExtra(AppConstant.CAMERA_INDEX,index);
        setResult(RESULT_OK,intent);
        finish();
    }

    @Override
    public void editNameFailure(String msg) {
        UIUtils.showToast(msg);
    }
}
